package com.carkeeper.discover.service.impl;

import com.carkeeper.discover.mapper.BlogCategoryMapper;
import com.carkeeper.discover.service.BlogCategoryService;
import com.carkeeper.pojo.BlogCategory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
public class BlogCategoryServiceImpl implements BlogCategoryService {
    @Autowired
    private BlogCategoryMapper blogCategoryMapper;
    /**
     * 查询所有文稿分类
     */
    @Override
    public List<BlogCategory> findAll() {
        return blogCategoryMapper.selectAll();
    }
    /**
     * 根据id查询文稿分类
     */
    @Override
    public BlogCategory findById(Integer id){
        BlogCategory blogCategory = blogCategoryMapper.selectByPrimaryKey(id);
        return blogCategory;
    }

    /**
     * 根据名称查id
     * @param categoryName
     * @return
     */
    @Override
    public Integer findIdByName(String categoryName) {
     Integer id =blogCategoryMapper.findIdByName(categoryName);

     return id;
    }

    /**
     * 分页查询所有
     * @return
     */
    @Override
    public PageInfo<BlogCategory> findPage(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BlogCategory> list =this.findAll();
        PageInfo<BlogCategory> info=new PageInfo<>(list);
        return info;
    }
    /**
     * 添加
     * @param blogCategory
     */
    @Transactional
    @Override
    public void add(BlogCategory blogCategory) {
        blogCategory.setCreateTime(new Date());
        blogCategory.setUpdateTime(new Date());
        blogCategoryMapper.insertSelective(blogCategory);
    }

    /**
     * 编辑
     * @param blogCategory
     */
    @Transactional
    @Override
    public void update(BlogCategory blogCategory) {
        blogCategory.setUpdateTime(new Date());
        blogCategoryMapper.updateByPrimaryKeySelective(blogCategory);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Integer id) {
        blogCategoryMapper.deleteByPrimaryKey(id);
    }
}
